﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Globalization;

namespace LowLevelGraphics.ImageFormats
{
    /// <summary>
    /// PPM
    /// </summary>
    public class PPM : AbstractImageFormat, IImageFormat
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PPM"/> class.
        /// </summary>
        public PPM()
        {
        }

        //public static UnsafeBitmap Save(string _sFileName, UnsafeBitmap bitmap)
        //{
        //    //Use a streamwriter to write the text part of the encoding
        //    var writer = new StreamWriter(file);
        //    writer.Write("P6" + Environment.NewLine);
        //    writer.Write(bitmap.Width + " " +bitmap.Height + Environment.NewLine);
        //    writer.Write("255" + Environment.NewLine);
        //    writer.Close();
        //    //Switch to a binary writer to write the data
        //    var writerB = new BinaryWriter(new FileStream(file, FileMode.Append));
        //    for(int x=0; x<bitmap.Width;x++)
        //        for (int y = 0; y < bitmap.Height; y++)
        //        {
        //            var color = bitmap.GetPixel(x, y);
        //            writerB.Write(color.Red);
        //            writerB.Write(color.Green);
        //            writerB.Write(color.Blue);
        //        }
        //    writerB.Close();
        //}

        /// <summary>
        /// returns Extension
        /// </summary>
        public override string Extension
        {
            get { return "PPM"; }
        }

        /// <summary>
        /// Creates the specified _s file name.
        /// </summary>
        /// <param name="_sFileName">Name of the _s file.</param>
        /// <returns></returns>
        public static UnsafeBitmap Create(string _sFileName)
        {
            StreamReader reader = new StreamReader(_sFileName);

            string sText = reader.ReadToEnd();

            string[] aLine = Regex.Split(sText, "\n|\t|\v\f\r ");
            string sSubFormat = aLine[0];
            string sWidthHeight = aLine[1];

            string[] aWidthHeight = aLine[1].Split(' ');
            int nWidth = Convert.ToInt32(aWidthHeight[0], CultureInfo.InvariantCulture);
            int nHeight = Convert.ToInt32(aWidthHeight[1], CultureInfo.InvariantCulture);
            string sMaxValue = aLine[2];

            //if value is smaller than 256 use 2
            int nByte = Convert.ToInt32(sMaxValue, CultureInfo.InvariantCulture) < 256 ? 1 : 2;

            int x = 0;
            int y = 0;

            int nOffset = aLine[0].Length + aLine[1].Length + aLine[2].Length;
            //
            nOffset += 3;
            Color color = Color.Transparent;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            byte[] aByte = Encoding.Default.GetBytes(sText.Substring(nOffset, sText.Length - nOffset));
            if (aByte.Length / 3.0 != aByte.Length / 3)
            {
                Debug.WriteLine("Bytelength is invalid. Should be dividable by 3");
            }

            int i = nOffset;
            for (x = 0; x < nWidth; x++)
            {
                for (y = 0; y < nHeight; y++)
                {                    
                    color = Color.FromArgb(aByte[i++], aByte[i++], aByte[i++]);
                }
                bitmap.SetPixel(x, y, color);

            }
            return bitmap;
        }

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_sFileName"></param>
        /// <returns></returns>
        public override bool Save(string _sFileName)
        {
            throw new NotImplementedException();
        }
    }
}
